import sun.reflect.generics.tree.Tree;

import java.util.*;

//import static com.sun.glass.ui.EventLoop.stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2024-02-03
 * Time:19:40
 */
public class LevelOrder {
    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }

    }
    Stack<List<TreeNode>> stack1 = new Stack<>();

    public List<List<Integer>> levelOrderBottom(TreeNode root) {

        if(root == null)
            return null;
        Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> ret = new ArrayList<>();

        TreeNode cur = root;
        queue.offer(cur);

        while(!queue.isEmpty()) {
            List<Integer> list = new ArrayList<>();
            int size = queue.size();
            cur = queue.poll();
            list.add(cur.val);
            while (size > 0) {
                if(cur.left != null) {
                    queue.offer(cur.left);
                }
                if(cur.right != null) {
                    queue.offer(cur.right);
                }
                --size;
            }
            ret.add(list);
        }

        List<List<Integer>> result = new ArrayList<>();
        for(int i = ret.size()-1;i >= 0;i--) {
            List<Integer> tmp = ret.get(i);
            result.add(tmp);
        }
        return result;
    }

    public void levelOrder(TreeNode root) {
    }


}